# -*- coding:utf-8 -*-

from typing import List
from datetime import datetime, time
from module_admin.entity.do.role_do import SysRoleDept
from sqlalchemy import and_, delete, desc, func, or_, select, update
from sqlalchemy.ext.asyncio import AsyncSession
from module_gen.constants.gen_constants import GenConstants

from module_stock.entity.do.daily_do import Daily
from module_stock.entity.vo.daily_vo import DailyPageModel, DailyModel
from utils.page_util import PageUtil, PageResponseModel


class DailyDao:

    @classmethod
    async def get_by_id(cls, db: AsyncSession, daily_id: int) -> Daily:
        """根据主键获取单条记录"""
        daily = (((await db.execute(
                            select(Daily)
                            .where(Daily.id == daily_id)))
                       .scalars())
                       .first())
        return daily

    """
    查询
    """
    @classmethod
    async def get_daily_list(cls, db: AsyncSession,
                             query_object: DailyPageModel,
                             data_scope_sql: str = None,
                             is_page: bool = False) -> [list | PageResponseModel]:

        query = (
            select(Daily)
            .where(
                Daily.amount == query_object.amount if query_object.amount else True,
                Daily.change == query_object.change if query_object.change else True,
                Daily.close == query_object.close if query_object.close else True,
                Daily.high == query_object.high if query_object.high else True,
                Daily.low == query_object.low if query_object.low else True,
                Daily.open == query_object.open if query_object.open else True,
                Daily.pct_chg == query_object.pct_chg if query_object.pct_chg else True,
                Daily.pre_close == query_object.pre_close if query_object.pre_close else True,
                Daily.trade_date == query_object.trade_date if query_object.trade_date else True,
                Daily.ts_code == query_object.ts_code if query_object.ts_code else True,
                Daily.vol == query_object.vol if query_object.vol else True,
                eval(data_scope_sql) if data_scope_sql else True,
            )
            # .order_by(desc(Daily.create_time))
            # .distinct()
        )
        daily_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)
        return daily_list


    @classmethod
    async def add_daily(cls, db: AsyncSession, add_model: DailyModel, auto_commit: bool = True) -> Daily:
        """
        增加
        """
        daily =  Daily(**add_model.model_dump(exclude_unset=True))
        db.add(daily)
        await db.flush()
        if auto_commit:
            await db.commit()
        return daily

    @classmethod
    async def edit_daily(cls, db: AsyncSession, edit_model: DailyModel, auto_commit: bool = True) -> Daily:
        """
        修改
        """
        edit_dict_data = edit_model.model_dump(exclude_unset=True, exclude={*GenConstants.DAO_COLUMN_NOT_EDIT})
        await db.execute(update(Daily), [edit_dict_data])
        await db.flush()
        if auto_commit:
            await db.commit()
        return await cls.get_by_id(db, edit_model.id)

    @classmethod
    async def del_daily(cls, db: AsyncSession, daily_ids: List[str], soft_del: bool = True, auto_commit: bool = True):
        """
        删除
        """
        if soft_del:
            await db.execute(update(Daily).where(Daily.id.in_(daily_ids)).values(del_flag='2'))
        else:
            await db.execute(delete(Daily).where(Daily.id.in_(daily_ids)))
        await db.flush()
        if auto_commit:
            await db.commit()